package cc.yiueil.controller;

import cc.yiueil.entity.OrderEntity;
import cc.yiueil.feign.WeatherClient;
import cc.yiueil.properties.OrderProperties;
import cc.yiueil.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

// 此注解 用于自动刷新配置中心的配置, 以便于在不重启的情况下, 从配置中心更新数据
@RefreshScope
@RestController
@EnableConfigurationProperties(OrderProperties.class)
public class OrderController {

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @Value(value = "${order.timeout}")
    private String timeout;

    @Value(value = "${order.auto-confirm}")
    private String confirm;

    @Autowired
    private OrderProperties orderProperties;

    @Autowired
    private WeatherClient weatherClient;


    @GetMapping(value = "/createOrder")
    public OrderEntity createOrder(@RequestParam Long userId, @RequestParam Long productId) {
        return orderService.createOrder(productId, userId);
    }

    @GetMapping(value = "/testOrder")
    public OrderEntity testOrder() {
        return orderService.testOrder();
    }

    @GetMapping(value = "/getCurrentWeather")
    public String getCurrentWeather(@RequestParam(value = "key") String key, @RequestParam(value = "location") String location) {
        return weatherClient.getCurrentWeather(key, location);
    }

    @GetMapping(value = "/getConfig")
    public String getConfig(){
        return String.format("%s, %s", confirm, timeout);
    }

    @GetMapping(value = "/getConfigByProperties")
    public String getConfigByProperties(){
        return orderProperties.toString();
    }

}
